package com.c.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.c.entity.Menu;
import com.c.entity.Role;
import com.c.mapper.MenuMapper;
import com.c.service.MenuService;
import com.c.utils.MessageConstant;
import com.c.utils.PageResult;
import com.c.utils.QueryPageBean;
import com.c.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wangxiangli
 * @create 2022-12-26-15:30
 */
@Service
@Transactional
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public List<Menu> findMenuByUsername(String username) {

        List<Menu> menus = baseMapper.findMenuByUsername(username);
        return menus;
    }

    /**
     * 回显角色菜单信息
     * @param id
     * @return
     */
    @Override
    public Integer[] findMenuIdsByRoleId(Integer id) {
        return baseMapper.findMenuIdsByRoleId(id);
    }

    /**
     * 菜单分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        IPage<Menu> pageMenu=new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //忽略空格查询条件
        if (queryPageBean.getQueryString() != null && queryPageBean.getQueryString().length() > 0) {
            queryString = queryPageBean.getQueryString().trim();// 去除查询条件首尾空格
        }
        IPage<Menu> page = baseMapper.getTreeData(pageMenu,queryString);
        return new PageResult(page.getTotal(), page.getRecords());
    }

    /**
     * 查询父菜单信息
     *
     * @return
     */
    @Override
    public List<Menu> findAllParent() {
        return baseMapper.findAllParent();
    }

    /**
     * 新增菜单
     *
     * @param menu
     * @return
     */
    @Override
    public Result add(Menu menu) {
        //先判断菜单关键字是否存在（t_permission）
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("name",menu.getName());
        wrapper.eq("linkUrl",menu.getLinkUrl());
        wrapper.eq("parentMenuId",menu.getParentMenuId());
        Menu menu1 = baseMapper.selectOne(wrapper);
        if (menu1 != null) {
            return new Result(false, "该权限菜单已经存在,无法创建");
        } else {
            if(menu.getParentMenuId()!=null){
                menu.setLevel(2);
            }else {
                menu.setLevel(1);
            }
            baseMapper.insert(menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        }
    }

    /**
     * 编辑菜单
     *
     * @param menu
     */
    @Override
    public void edit(Menu menu) {
        if(menu.getParentMenuId()!=null){
            menu.setLevel(2);
        }else {
            menu.setLevel(1);
        }
       baseMapper.updateById(menu);
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        //判断当前菜单是否存在子菜单
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("parentMenuId",id);
        Long count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            //存在子菜单，不允许删除
            return new Result(false, "要删除的菜单存在子菜单,无法删除！");
        }
        //删除 t_role_menu 表中的菜单数据
        baseMapper.deleteRoleMenuById(id);
        //删除 t_menu 表
        baseMapper.deleteById(id);
        return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
    }
}
